$(function(){
	$("#allupdateSql").on('click',function(){
		$.post(contextPath + "/quota/allUpdateQuotaSql.dhc").done(function(data){})
	})
	loadQuotaSettings();
})
/**
 * @author ：LiuHongjun
 * @any
 * @Discription:加载指标数据
 * @quota-setting.js
 */
function loadQuotaSettings(){
	$.post(contextPath + "/quota/getQuotaList.dhc").done(function(data){
		var columns=[
						{
							"data": null,
							"className": 'details-control'
						},
						{
							"title":"指标代码",
							"data": "QUOTA_CODE"
						},
						{
							"title":"指标名称",
							"data": "TEXT"
						},
						{
							"title":"指标类型",
							"data": "TYPE",
							"render": function(obj, type, full){
								var type=full.TYPE;
								var qoutaType;
								if(type=='0'){
									qoutaType='Base指标';
								}else if(type=='1'){
									qoutaType='大类指标';
								}else if(type=='2'){
									qoutaType='单指标';
								}else if(type=='3'){
									qoutaType='绩效值、标杆值';
								}else if(type=='4'){
									qoutaType='关联指标';
								}else if(type=='5'){
									qoutaType='专科建设相关指标';
								}else if(type=='6'){
									qoutaType='历史数据查询指标';
								}
								return qoutaType ;
							}
						},
						{
							"title":"精度",
							"data": "UNIT"
						},{
							"title":"是否监测页面指标",
							"data": "QUOTA_IS_PAGES_o",
							"render": function(obj, type, full){
								var qoutaType;
								var page=full.QUOTA_IS_PAGES;
								if(page=='0'){
									qoutaType='否';
								}else if(page=='1'){
									qoutaType='是';
								}else if(page=='2'){
									qoutaType='未定义';
								}
								return qoutaType ;}
						},{
							"title":"是否指标",
							"data": "IS_QUOTA",
							"render": function(obj, type, full){
								if(obj==1){
									return '是';
								}
								else{
									return '否';
								}
							}
						},
						{
							"title":"备注",
							"data": "MARK"
						},{
							"title":"操作",
							"data": "operation",
							"render": function(obj, type, full){
								return "<a href=\"javascript:delQuota('"+full.QUOTA_CODE+"')\">删除</a>";
							}
						}
					];
		var paramsDataTables={info:false};

		paramsDataTables.buttons=[ {
            text: '新 增',
            action: function () { 
            	addQuota() ;
            }
        }];
	
		var oaTable = $("#"+tabId+" #quota_setting").dataTableBasic(columns,data.data,paramsDataTables);
		
		$("#"+tabId+" #quota_setting>tbody").off("click", "td.details-control");
		$("#"+tabId+" #quota_setting>tbody").on('click', 'td.details-control', function () {
			var nTr = $(this).closest('tr');
			var row = oaTable.row( nTr );
			if (row.child.isShown()) //判断是否已打开
			{
				row.child.hide();
				nTr.removeClass("shown");
			} else {
				nTr.addClass("shown");
				row.child("正在加载...").show();
				try{
					var quotaType = row.data().IS_QUOTA;
					var type = row.data().TYPE;
					var quotaDesc=row.data().TEXT;
					if(quotaType == "1") {
						$.post(contextPath + "/quota/getQuotaByCode.dhc", {quotaCode: row.data().QUOTA_CODE}).done(function(data){
							var quota={};
							var data = data.data;
							if(data.length>0){
								quota = data[0];
							}
							var str = '<div style="width:50%">';
							str+=' <table width="100%">  ';
							str+=' <tr>';
							str+=' <td>指标代码：<input id="QUOTACODE" value="'+quota["QUOTA_CODE"]+'" disabled/></td>';
							str+=' <td>指标名称：<input id="QUOTANAME" value="'+quota["TEXT"]+'" /></td>';
							str+=' </tr>';
							str+=' <tr>';
							str+=' <td>是否指标：<select id="IS_QUOTA"><option value="1" '+(quota["IS_QUOTA"]=="1"?"selected":"")+'>是</option><option value="0" '+(quota["IS_QUOTA"]!="1"?"selected":"")+'>否</option></select></td>';
							str+=' <td>是否有超链接：<select id="IS_LINK"><option value="1" '+(quota["IS_LINK"]=="1"?"selected":"")+'>是</option><option value="0" '+(quota["IS_LINK"]!="1"?"selected":"")+'>否</option></select></td>';
							str+=' </tr>';
							str+=' <tr>';
							str+=' <td>数据类型：<select id="Q_TYPE"><option value="ratio" '+(quota["Q_TYPE"]=="ratio"?"selected":"")+'>比率类</option><option value="other" '+(quota["Q_TYPE"]!="ratio"?"selected":"")+'>其他</option></select></td>';
							str+=' <td>小数位数：<input id="UNIT" value="'+(quota["UNIT"]==null?"":quota["UNIT"])+'" /></td>';
							str+=' </tr>';
							str+=' <tr>';
							str+=' <td>指标类型：<select id="QUOTA_TYPE_OLD"></select></td>';
							str+=' <td>是否监测页面指标：<select id="QUOTA_IS_PAGES"></select></td>';
							str+=' </tr>';
							str+=' <tr>';
							str+=' <td>备注：<input id="MARK" value="'+(quota["MARK"]==null?"":quota["MARK"])+'" /></td>';
							str+=' <td></td>';
							str+=' </tr>';
							str+=' <td colspan=2>sql：<textarea id="sql" wrap="hard" style="width:600px;height:280px;">'+(quota["SELECT_SQL"]==null?"":quota["SELECT_SQL"])+'</textarea></td>';
							str+=' </tr>';
							str+=' </table>';
							str+=' <button id="saveQuotaBtn">保存</button>';
							str+='</div>';
							row.child(str).show();
							for(var o in diction.QUOTA_TYPE_OLD){
								row.child().find('#QUOTA_TYPE_OLD').append('<option value="'+diction.QUOTA_TYPE_OLD[o].DICVALUE+'">'+diction.QUOTA_TYPE_OLD[o].DICDESC+'</option>')
							}
							
							for(var o in diction.QUOTA_IS_PAGES){
								row.child().find('#QUOTA_IS_PAGES').append('<option value="'+diction.QUOTA_IS_PAGES[o].DICVALUE+'">'+diction.QUOTA_IS_PAGES[o].DICDESC+'</option>')
							}
							var value=quota["QUOTA_IS_PAGES"]==null?0:quota["QUOTA_IS_PAGES"];
							if(value==null){
								row.child().find('#QUOTA_IS_PAGES option[value="2"]').attr("selected",true);
							}else{
								row.child().find('#QUOTA_IS_PAGES option[value="'+value+'"]').attr("selected",true);

							}
							var value=quota["TYPE"]==null?0:quota["TYPE"];
							row.child().find('#QUOTA_TYPE_OLD option[value="'+value+'"]').attr("selected",true);
							
							for(var o in diction.QUOTA_TYPE){
								row.child().find('#QUOTA_TYPE').append('<option value="'+diction.QUOTA_TYPE[o].DICVALUE+'">'+diction.QUOTA_TYPE[o].DICDESC+'</option>')
							}
							var value=quota["QUOTA_TYPE"]==null?0:quota["QUOTA_TYPE"];
							row.child().find('#QUOTA_TYPE option[value="'+value+'"]').attr("selected",true);
							for(var o in diction.QUOTA_TYPE_DTL){
								row.child().find('#QUOTA_TYPE_DTL').append('<option value="'+diction.QUOTA_TYPE_DTL[o].DICVALUE+'">'+diction.QUOTA_TYPE_DTL[o].DICDESC+'</option>')
							}
							var value=quota["QUOTA_TYPE_DTL"]==null?0:quota["QUOTA_TYPE_DTL"];
							row.child().find('#QUOTA_TYPE_DTL option[value="'+value+'"]').attr("selected",true);
							
							row.child().find('#QUOTA_TYPE_OLD').change(function() {
								if($(this).val() == 4 || $(this).val() == 1) {
									drgUI.warning('选择该类指标保存后将不可更改指标类型!') ;
									row.child().find('#Q_TYPE option[value="0"]').attr("selected",true) ;
									row.child().find('#Q_TYPE').attr("disabled",true) ;
									row.child().find('#UNIT').attr("value",0) ;
									row.child().find('#UNIT').attr("disabled",true) ;
									row.child().find('#IS_LINK option[value="0"]').attr("selected",true) ;
									row.child().find('#IS_LINK').attr("disabled",true) ;
									row.child().find('#IS_QUOTA option[value="0"]').attr("selected",true) ;
									row.child().find('#IS_QUOTA').attr("disabled",true) ;
									row.child().find('#QUOTA_IS_PAGES option[value="0"]').attr("selected",true) ;
									row.child().find('#QUOTA_IS_PAGES').attr("disabled",true) ;
									if(row.child().find('#sql').val()!="") {
										drgUI.warning('请将指标SQL清空!') ;
										return ;
									}
									row.child().find('#sql').attr("disabled",true) ;
								} else {
									row.child().find('#Q_TYPE').removeAttr("disabled") ;
									row.child().find('#UNIT').removeAttr("disabled") ;
									row.child().find('#IS_LINK').removeAttr("disabled") ;
									row.child().find('#IS_QUOTA').removeAttr("disabled") ;
									row.child().find('#QUOTA_IS_PAGES').removeAttr("disabled") ;
									row.child().find('#sql').removeAttr("disabled") ;
								}
							})
							
							row.child().find("#saveQuotaBtn").click(function(){
								row.child().find("#saveQuotaBtn").attr("disabled", "disabled");
								var quota={
										quotaCode: row.child().find("#QUOTACODE").val(),
										quotaName: row.child().find("#QUOTANAME").val(),
										quotaType: row.child().find("#QUOTA_TYPE").val(),
										quotaTypeDtl: row.child().find("#QUOTA_TYPE_DTL").val(),
										qType: row.child().find("#Q_TYPE").val(),
										unit: row.child().find("#UNIT").val(),
										isLink: row.child().find("#IS_LINK").val(),
										isQuota:row.child().find("#IS_QUOTA").val(),
										sql: row.child().find("#sql").val(),
										type:row.child().find("#QUOTA_TYPE_OLD").val(),
										mark: row.child().find("#MARK").val(),
										QUOTA_IS_PAGES:row.child().find("#QUOTA_IS_PAGES").val()
									};
								$.post(contextPath + "/quota/saveQuota.dhc", {param: JSON.stringify(quota)}).done(function(data){
									if(!quota.quotaCode || quota.quotaCode==""){
										drgUI.warning("指标代码不能为空!");
									}
									$.post(contextPath + "/quota/saveQuota.dhc", {param: JSON.stringify(quota)}).done(function(data){
										if(!data.success){
											var errorLog = "指标("+quota.quotaCode+")报错\n"+data.errorMessage;
											drgUI.error(errorLog) ;
										}else{
											if(data.data.result=='1'){
												drgUI.warning("此指标已存在，已更新为您新添加的信息！")	;
											}else{
												drgUI.success("保存成功！")	;	
											}
											loadQuotaSettings();
										}
										row.child().find("#saveQuotaBtn").removeAttr("disabled");
									})
								})
							})
						})
					} else if (type == "4") {
						quotaCode=row.data().QUOTA_CODE;
						$.post(contextPath + "/quota/getQuotaRelease.dhc", {quotaCode: quotaCode}).done(function(data){
							var str = '';
							str+='<div style="width:50%;">';
							str+='<div class="clear">关联指标详情</div>';
							str+=' <table>  ';
							str+='<thead>'
							str+=' <tr>';
							str+=' <th></th>';
							str+=' <th>指标代码</th>';
							str+=' <th>指标名称</th>';
							str+=' <th></th>';
							str+=' </tr>';
							str+='</thead>';
							str+='<tbody></tbody>';
							str+=' </table>';
							str+=' <button id="saveQuotaBtn">保存</button>';
							str+='</div>';
							var columns=[
											{
												"data": 'addReleaseQuota',
												"className": 'add_release'
											},
											{
												"data": "quotaCode"
											},
											{
												"data": "quotaName"
											},
											{
												"data": 'delReleaseQuota',
												"className": 'del_release'
											}
											];
							row.child(str).show();
							var paramsDataTables={info:false,lengthChange:false,searching:false,ordering:false,tableTitle:quotaDesc+'详情'};
							var oaTablec = row.child().find("div>table").dataTableBasic(columns,data.data,paramsDataTables);
							var quotaReleaseList = [];
							$.post(contextPath + "/quota/getQuotaReleaseList.dhc", {quotaType: "0"}).done(function(data_1){
								quotaReleaseList = data_1.data;
								if(data.data.length==0){
									row.child().find("div table>tbody>tr").remove();
									var nNewRow = addTr();
									row.child().find("div table>tbody").append(nNewRow);
									var option = {
											onChange:function(n, o){
												$(this).parent().prev().find("input").val(n)
											}
										}
									$CommonUI.combobox(row.child().find("div table>tbody>tr").find("td:eq(2)>input"),quotaReleaseList,"TEXT","QUOTA_CODE", option);
								}
							});
							row.child().find("div table>tbody").off("click", "td.add_release");
							row.child().find("div table>tbody").on('click', 'td.add_release', function () {
								var nTr1 = $(this).closest('tr');
								var nNewRow = addTr();
								$(nTr1).after(nNewRow);
								var option = {
									onChange:function(n, o){
										$(this).parent().prev().find("input").val(n);
									}
								}
								$CommonUI.combobox($(nTr1).next().find("td:eq(2)>input"),quotaReleaseList,"TEXT","QUOTA_CODE", option);
							});
							
							function addTr(){
								var nNewRow = document.createElement("tr");
								var nNewCell1 = document.createElement("td");
								nNewCell1.setAttribute("class", "add_release sorting_1");
								nNewRow.appendChild( nNewCell1 );
								
								var nNewCell2 = document.createElement("td");
								var nNewInput2 = document.createElement("input");
								nNewCell2.appendChild(nNewInput2);
								nNewRow.appendChild( nNewCell2 );
								
								var nNewCell3 = document.createElement("td");
								var nNewInput3 = document.createElement("input");
								nNewInput3.setAttribute("id", "ast1");
								nNewCell3.appendChild(nNewInput3);
								nNewRow.appendChild( nNewCell3 );
								
								var nNewCell4 = document.createElement("td");
								nNewCell4.setAttribute("class", "del_release sorting_1");
								nNewRow.appendChild( nNewCell4 );
								return nNewRow;
							}
							
							row.child().find("div table>tbody").off("click", "td.del_release");
							row.child().find("div table>tbody").on('click', 'td.del_release', function () {
								var nTr1 = $(this).closest('tr');
								$(nTr1).remove();
							});
							row.child().find("#saveQuotaBtn").click(function(){
								row.child().find("#saveQuotaBtn").attr("disabled", "disabled");
								var quotas = [];
								for(var o=0; o<row.child().find("div table>tbody").find("tr").size(); o++){
									var tr = row.child().find("div table>tbody").find("tr").eq(o);
									var quotaCode_1 = $(tr).find("td:eq(1)").html();
									if($(tr).find("td:eq(1)>input").size()>0){
										quotaCode_1 = $(tr).find("td:eq(1)>input").val();
									}
									var quotaName_1 = $(tr).find("td:eq(2)").html();
									if($(tr).find("td:eq(2)>input").size()>0){
										quotaName_1 = $(tr).find("td:eq(2) input.combobox-f").combobox("getText");
									}
									var quota = {"quotaCode": quotaCode_1, "quotaName": quotaName_1, "quotaId": quotaCode, "quotaIndex": o}
									if(quota.quotaCode){
										quotas.push(quota);
									}
								}
								//清除无效的关联指标
								var invalidReleaseQuotas = " ";
								for(var o in quotas){
									var quota = quotas[o];
									invalidReleaseQuotas += "'"+quota.quotaCode+"',"
								}
								invalidReleaseQuotas = invalidReleaseQuotas.substring(0, invalidReleaseQuotas.length-1);
								$.post(contextPath + "/quota/delInvalidReleaseQuotas.dhc", {param: invalidReleaseQuotas, quotaCode: quotaCode}).done(function(data){
									if(!data.success){
										var errorLog = "保存失败,日志："+data.errorMessage;
										drgUI.error(errorLog) ;
									}else{
										var ajaxCount = quotas.length;
										var ajaxS = 0;
										var errorLog='';
										for(var o in quotas){
											var quota = quotas[o];
											$.post(contextPath + "/quota/saveQuotaRelease.dhc", {"param": JSON.stringify(quota)}).done(function(){
												ajaxS ++;
												if(!data.success){
													errorLog += "指标("+node.code+")报错\n"+data.errorMessage;
													errorLog += "\n";
												}
											});
										}
										var timer = setInterval(function(){
											if(ajaxS == ajaxCount){
												clearInterval(timer);
												if(errorLog){
													drgUI.error(errorLog) ;
												}else{
													drgUI.success('保存成功');
												}
												row.child().find("#saveQuotaBtn").removeAttr("disabled");
											}
										}, 500);
									}
								});
								
							})
						});
					} else {
						$.post(contextPath + "/quota/getQuotaByCode.dhc", {quotaCode: row.data().QUOTA_CODE}).done(function(data){
							var quota={};
							var data = data.data;
							if(data.length>0){
								quota = data[0];
							}
							var str = '<div style="width:50%">';
							str+=' <table width="100%">  ';
							str+=' <tr>';
							str+=' <td>指标代码：<input id="QUOTACODE" value="'+quota["QUOTA_CODE"]+'" disabled/></td>';
							str+=' <td>指标名称：<input id="QUOTANAME" value="'+quota["TEXT"]+'" /></td>';
							str+=' </tr>';
							str+=' </table>';
							str+=' <button id="saveQuotaBtn">保存</button>';
							str+='</div>';
							row.child(str).show();
							row.child().find("#saveQuotaBtn").click(function(){
								row.child().find("#saveQuotaBtn").attr("disabled", "disabled");
								var quota={
										quotaCode: row.child().find("#QUOTACODE").val(),
										quotaName: row.child().find("#QUOTANAME").val()
									};
								$.post(contextPath + "/quota/saveQuota.dhc", {param: JSON.stringify(quota)}).done(function(data){
									if(!quota.quotaCode || quota.quotaCode==""){
										drgUI.error('指标代码不能为空!') ;
									}
									$.post(contextPath + "/quota/saveQuota.dhc", {param: JSON.stringify(quota)}).done(function(data){
										if(!data.success){
											var errorLog = "指标("+quota.quotaCode+")报错\n"+data.errorMessage;
											drgUI.error(errorLog) ;
										}else{
											if(data.data.result=='1'){
												drgUI.warning("此指标已存在，已更新为您新添加的信息!")	;
											}else{
												drgUI.success("保存成功！")	;	
											}
											loadQuotaSettings();
										}
										row.child().find("#saveQuotaBtn").removeAttr("disabled");
									})
								})
							})
						})
					}
				}catch(e){
					$(nTr).next().find("td").html("出错了...");
				}
			}
	    });
	})
}
/**
 * @author ：LiuHongjun
 * @any
 * @Discription:添加弹出框
 * @quota-setting.js
 */
function addQuota(){
	$.post(contextPath+"/pages/common/top-dialog-buttons.jsp").done(function(data){
		var dialog  = $(data);
		dialog.find(".modal-dialog").css("width", "700px");
		dialog.find(".echartscq").css("width", "680px");
		dialog.find(".echartscq").css("height","360px") ;
		dialog.find(".modal-title").html("增加指标");
		var str = '';
		str+=' <table width="100%" style="border-collapse:separate; border-spacing:0px 4px;">  ';
		str+=' <tr>';
		str+=' <td align="right" style="font-weight:bold">指标代码：</td><td><input id="QUOTACODE" /></td>';
		str+=' <td align="right" style="font-weight:bold">指标名称：</td><td><input id="QUOTANAME" /></td>';
		str+=' </tr>';
		str+=' <tr>';
		str+=' <td align="right" style="font-weight:bold">是否指标：</td><td><select id="IS_QUOTA"><option value="1" selected>是</option><option value="0">否</option></select></td>';
		str+=' <td align="right" style="font-weight:bold">是否有超链接：</td><td><select id="IS_LINK"><option value="1">是</option><option value="0" selected>否</option></select></td>';
		str+=' </tr>';
		str+=' <tr>';
		str+=' <td align="right" style="font-weight:bold">数据类型：</td><td><select id="Q_TYPE"><option value="ratio">比率类</option><option value="other">其他</option></select></td>';
		str+=' <td align="right" style="font-weight:bold">小数位数：</td><td><input id="UNIT" /></td>';
		str+=' </tr>';
		str+=' <tr>';
		str+=' <td align="right" style="font-weight:bold">指标类型：</td><td><select id="QUOTA_TYPE_OLD"></select></td>';
		str+=' <td align="right" style="font-weight:bold">是否监测页面指标：</td><td><select id="QUOTA_IS_PAGES"></select></td>';
		str+=' </tr>';
		str+=' <tr>';
		str+=' <td align="right" style="font-weight:bold">备注：</td><td><input id="MARK"/></td>';
		str+=' <td></td><td></td>';
		str+=' </tr>';
		str+=' <tr>';
		str+=' <td style="padding: 5px;font-weight:bold" align="right" >sql:</td><td colspan=4 ><textarea id="sql" wrap="hard" style="width:550px;height:200px;"></textarea></td>';
		str+=' </tr>';
		str+=' </table>';
		dialog.find(".echartscq").html(str);
		dialog.find(".echartscq table tr td").css({'padding':'5px'});
		for(var o in diction.QUOTA_TYPE_OLD){
			dialog.find('#QUOTA_TYPE_OLD').append('<option value="'+diction.QUOTA_TYPE_OLD[o].DICVALUE+'">'+diction.QUOTA_TYPE_OLD[o].DICDESC+'</option>')
		}
		for(var o in diction.QUOTA_IS_PAGES){
			dialog.find('#QUOTA_IS_PAGES').append('<option value="'+diction.QUOTA_IS_PAGES[o].DICVALUE+'">'+diction.QUOTA_IS_PAGES[o].DICDESC+'</option>')
		}
		
		dialog.find('#QUOTA_TYPE_OLD').change(function() {
			if($(this).val() == 4 || $(this).val() == 1) {
				drgUI.warning('选择该类指标保存后将不可更改指标类型!') ;
				dialog.find('#Q_TYPE option[value="0"]').attr("selected",true) ;
				dialog.find('#Q_TYPE').attr("disabled",true) ;
				dialog.find('#UNIT').attr("value",0) ;
				dialog.find('#UNIT').attr("disabled",true) ;
				dialog.find('#IS_LINK option[value="0"]').attr("selected",true) ;
				dialog.find('#IS_LINK').attr("disabled",true) ;
				dialog.find('#IS_QUOTA option[value="0"]').attr("selected",true) ;
				dialog.find('#IS_QUOTA').attr("disabled",true) ;
				dialog.find('#QUOTA_IS_PAGES option[value="0"]').attr("selected",true) ;
				dialog.find('#QUOTA_IS_PAGES').attr("disabled",true) ;
				dialog.find('#sql').attr("disabled",true) ;
			} else {
				dialog.find('#Q_TYPE').removeAttr("disabled") ;
				dialog.find('#UNIT').removeAttr("disabled") ;
				dialog.find('#IS_LINK').removeAttr("disabled") ;
				dialog.find('#IS_QUOTA').removeAttr("disabled") ;
				dialog.find('#QUOTA_IS_PAGES').removeAttr("disabled") ;
				dialog.find('#sql').removeAttr("disabled") ;
			}
		})
		
		dialog.find('#save').on('click', function(){
			var quota={
					quotaCode: dialog.find("#QUOTACODE").val(),
					quotaName: dialog.find("#QUOTANAME").val(),
					quotaType: 0,
					quotaTypeDtl: 0,
					qType: dialog.find("#Q_TYPE").val(),
					unit: dialog.find("#UNIT").val(),
					isLink: dialog.find("#IS_LINK").val(),
					isQuota:dialog.find("#IS_QUOTA").val(),
					mark: dialog.find("#MARK").val(),
					sql: dialog.find("#sql").val(),
					type:dialog.find("#QUOTA_TYPE_OLD").val(),
					QUOTA_IS_PAGES:dialog.find("#QUOTA_IS_PAGES").val()
			};
			if(!quota.quotaCode || quota.quotaCode==""){
				drgUI.warning('指标代码不能为空!') ;
			}
			$.post(contextPath + "/quota/saveQuota.dhc", {param: JSON.stringify(quota)}).done(function(data){
				if(!data.success){
					var errorLog = "指标("+quota.quotaCode+")报错\n"+data.errorMessage;
					drgUI.error(errorLog) ;
				}else{
					if(data.data.result=='1'){
						drgUI.warning("此指标已存在，已更新为您新添加的信息！");
					}else{
						drgUI.success("保存成功！")	;	
					}
					dialog.trigger('complete');
				}
			})
		}).end().modal({
			keyboard : false
		}).on({
			'hidden.bs.modal' : function() {
				dialog.remove();
			},
			'complete' : function() {
				dialog.modal('hide');
				loadQuotaSettings();
            }
		});
	});
}

/**
 * 
 * @author ：LiuHongjun
 * @any
 * @Discription:修改弹出框
 * @quota-setting.js
 */
function mergeQuota(quotaCode){
	$.post(contextPath+"/pages/common/top-dialog-buttons.jsp").done(function(data){
		var dialog  = $(data);
		dialog.find(".modal-title").html("修改指标");
		$.post(contextPath + "/quota/getQuotaByCode.dhc", {quotaCode: quotaCode}).done(function(data){
			var quota={};
			var data = data.data;
			if(data.length>0){
				quota = data[0];
			}
			var str = '';
			str+=' <table width="50%">  ';
			str+=' <tr>';
			str+=' <td>指标代码：<input id="QUOTACODE" value="'+quota["QUOTA_CODE"]+'" disabled/></td>';
			str+=' <td>指标名称：<input id="QUOTANAME" value="'+quota["TEXT"]+'" /></td>';
			str+=' </tr>';
			str+=' <tr>';
			str+=' <td>指标大类：<select id="QUOTA_TYPE"></select></td>';
			str+=' <td>指标子类：<select id="QUOTA_TYPE_DTL"></select></td>';
			str+=' </tr>';
			str+=' <tr>';
			str+=' <td>数据类型：<select id="Q_TYPE"><option value="ratio" '+(quota["Q_TYPE"]=="ratio"?"selected":"")+'>比率类</option><option value="other" '+(quota["Q_TYPE"]!="ratio"?"selected":"")+'>其他</option></select></td>';
			str+=' <td>小数位数：<input id="UNIT" value="'+(quota["UNIT"]==null?"":quota["UNIT"])+'" /></td>';
			str+=' </tr>';
			str+=' <tr>';
			str+=' <td>是否有超链接：<select id="IS_LINK"><option value="1" '+(quota["IS_LINK"]=="1"?"selected":"")+'>是</option><option value="0" '+(quota["IS_LINK"]!="1"?"selected":"")+'>否</option></select></td>';
			str+=' <td>是否指标：<select id="IS_QUOTA"><option value="1" '+(quota["IS_QUOTA"]=="1"?"selected":"")+'>是</option><option value="0" '+(quota["IS_QUOTA"]!="1"?"selected":"")+'>否</option></select></td>';
			str+=' </tr>';
			str+=' <tr>';
			str+=' <td>指标类型：<select id="QUOTA_TYPE_OLD"></select></td>';
			str+=' <td>是否监测页面指标：<select id="QUOTA_IS_PAGES"></select></td>';
			str+=' </tr>';
			str+=' <tr>';
			str+=' <td>备注：<input id="MARK" value="'+(quota["MARK"]==null?"":quota["MARK"])+'" /></td>';
			str+=' <td></td>';
			str+=' </tr>';
			str+=' <tr>';
			str+=' <td colspan=2>sql：<textarea id="sql" wrap="hard" style="width:600px;height:200px;">'+quota["SELECT_SQL"]+'</textarea></td>';
			str+=' </tr>';
			str+=' </table>';
			dialog.find(".echartscq").html(str);
			dialog.find(".echartscq table tr td").css({'padding':'5px'});
			for(var o in diction.QUOTA_TYPE_OLD){
				dialog.find('#QUOTA_TYPE_OLD').append('<option value="'+diction.QUOTA_TYPE_OLD[o].DICVALUE+'">'+diction.QUOTA_TYPE_OLD[o].DICDESC+'</option>')
			}
			for(var o in diction.QUOTA_IS_PAGES){
				dialog.find('#QUOTA_IS_PAGES').append('<option value="'+diction.QUOTA_IS_PAGES[o].DICVALUE+'">'+diction.QUOTA_IS_PAGES[o].DICDESC+'</option>')
			}
			var pages_value=quota["QUOTA_IS_PAGES"]==null?0:quota["QUOTA_IS_PAGES"];
			if(pages_value==null ){
				dialog.find('#QUOTA_IS_PAGES option[value="2"]').attr("selected",true);
			}else{
				dialog.find('#QUOTA_IS_PAGES option[value="'+pages_value+'"]').attr("selected",true);
			}
			var value=quota["TYPE"]==null?0:quota["TYPE"];
			if(value!=null){
				dialog.find('#QUOTA_TYPE_OLD option[value="'+value+'"]').attr("selected",true);
			}

			for(var o in diction.QUOTA_TYPE){
				dialog.find('#QUOTA_TYPE').append('<option value="'+diction.QUOTA_TYPE[o].DICVALUE+'">'+diction.QUOTA_TYPE[o].DICDESC+'</option>')
			}
			if(value!=null){
				var value=quota["QUOTA_TYPE"]==null?0:quota["QUOTA_TYPE"];
			}
			dialog.find('#QUOTA_TYPE option[value="'+value+'"]').attr("selected",true);
			for(var o in diction.QUOTA_TYPE_DTL){
				dialog.find('#QUOTA_TYPE_DTL').append('<option value="'+diction.QUOTA_TYPE_DTL[o].DICVALUE+'">'+diction.QUOTA_TYPE_DTL[o].DICDESC+'</option>')
			}
			if(value!=null){
				var value=quota["QUOTA_TYPE_DTL"]==null?0:quota["QUOTA_TYPE_DTL"];
			}			
			dialog.find('#QUOTA_TYPE_DTL option[value="'+value+'"]').attr("selected",true);			
		})
		dialog.find('#save').on('click', function(){
			dialog.find('#save').attr("disabled", "");
			var quota={
				quotaCode: dialog.find("#QUOTACODE").val(),
				quotaName: dialog.find("#QUOTANAME").val(),
				quotaType: dialog.find("#QUOTA_TYPE").val(),
				quotaTypeDtl: dialog.find("#QUOTA_TYPE_DTL").val(),
				qType: dialog.find("#Q_TYPE").val(),
				unit: dialog.find("#UNIT").val(),
				isLink: dialog.find("#IS_LINK").val(),
				isQuota:dialog.find("#IS_QUOTA").val(),
				sql: dialog.find("#sql").val(),
				mark: dialog.find("#MARK").val(),
				type:dialog.find("#QUOTA_TYPE_OLD").val(),
				QUOTA_IS_PAGES:dialog.find("#QUOTA_IS_PAGES").val()
			};
			$.post(contextPath + "/quota/saveQuota.dhc", {param: JSON.stringify(quota)}).done(function(data){
				if(!quota.quotaCode || quota.quotaCode==""){
					drgUI.error('指标代码不能为空!') ;
				}
				$.post(contextPath + "/quota/saveQuota.dhc", {param: JSON.stringify(quota)}).done(function(data){
					if(!data.success){
						var errorLog = "指标("+quota.quotaCode+")报错\n"+data.errorMessage;
						drgUI.error(errorLog) ;
					}else{
						drgUI.success('保存成功!') ;
						dialog.trigger('complete');
					}
				})
			})
		}).end().modal({
			keyboard : false
		}).on({
			'hidden.bs.modal' : function() {
				dialog.remove();
			},
			'complete' : function() {
				dialog.modal('hide');
				loadQuotaSettings();
            }
		});
	});
}

function delQuota(quotaCode){
	drgUI.confirm({
		content : '确定要删除指标吗?删除后,关联指标也将会删除',
		callSure : function() {
			$.post(contextPath + "/quota/delQuota.dhc", {quotaCode: quotaCode}).done(function(data){
				if(!data.success){
					drgUI.error( "删除失败,日志："+data.errorMessage) ;
				}else{
					drgUI.success('删除成功!') ;
					loadQuotaSettings();
				}
			});
		}
	});
}